//
//  ZZCChangePhoneTwoController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCChangePhoneTwoController.h"
#import "ZZCPropertyController.h"

@interface ZZCChangePhoneTwoController ()
@property (weak, nonatomic) IBOutlet UITextField *testTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (copy, nonatomic) NSString *code;
@end

@implementation ZZCChangePhoneTwoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"换绑手机号";
    [self.testTextField becomeFirstResponder];
}

//获取验证码
- (IBAction)testBtnClick {
    ZZCLog(@"获取验证码");
    __block int time = 60;
    __block UIButton *verifybutton = _codeBtn;
    verifybutton.enabled = NO;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
//                verifybutton.backgroundColor = [UIColor whiteColor];
                [verifybutton setTitle:@"获取验证码" forState:UIControlStateNormal];
                verifybutton.enabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
//                verifybutton.backgroundColor = [UIColor whiteColor];
                NSString *strTime = [NSString stringWithFormat:@"已发送(%d)",time];
                [verifybutton setTitle:strTime forState:UIControlStateNormal];
                verifybutton.titleLabel.textColor = [UIColor whiteColor];
            });
            time--;
        }
    });
    dispatch_resume(_timer);

    NSDictionary *dict = @{@"phone":self.phoneNumber,@"type":@(5)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:sign];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"phone":self.phoneNumber,@"type":@(5)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_SENDCODE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            self.code = responseObject[@"data"][@"code"];
        }
    }];

}
- (IBAction)gerCode:(UIButton *)btn {
}
//确认
- (IBAction)confirmClick {
     ZZCLog(@"你点击确认按钮");
    [self uploadRequest];
    
}
- (void)uploadRequest {
    if (self.phoneNumber == nil) {
        [SVProgressHUD showInfoWithStatus:@"请输入更改的昵称"];
        return;
    }
    if (self.passwordTextField.text == nil) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        return;
    }

    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,
                           @"phone":self.phoneNumber,
                           @"password":self.passwordTextField.text,
                           @"checkcode":self.code};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],
                            @"sign":sign,
                            @"token":token,
                            @"phone":self.phoneNumber,
                            @"password":self.passwordTextField.text,
                            @"checkcode":self.code};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_PHONE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:self.phoneNumber forKey:@"userphone"];
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changePhoneSucceed" object:nil];
                
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCPropertyController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        //这里可以做回到登录界面的一些数据操作

                    }
                }
                
            }
        }
    }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
